const express = require("express");
const url = require('url')
const config = require('../config/config')
var router = express.Router()

// 判断用户有没有登录的中间件
router.use((req, res, next) => {
    let pathname = url.parse(req.url).pathname

    // 判断用户是否有登录，有则直接next()
    if (req.session.userinfo && req.session.userinfo.username) {
        next()
    } else {

        // 没有登录，判断访问的url是否一下参数，否则会死循环
        // if (pathname == '/login' || pathname == '/login/doLogin' || pathname == '/login/verify') {
        //     next()
        // } else {

        //     // 用户没登陆强行跳转登录
        //     res.redirect(`/${config.adminPath}/login`)
        // }


        // 测试用，正式上线需要用到上述代码
        next()
    }
})

//引入模块
const main = require("./admin/main")
const user = require("./admin/user")
const login = require("./admin/login")
const nav = require("./admin/nav")
const manager = require("./admin/manager")
const focus = require("./admin/focus")
const articleCate = require("./admin/articleCate")
const article = require("./admin/article")
const setting = require("./admin/setting")


//挂载路由
router.use("/", main)
router.use("/user", user)
router.use("/login", login)
router.use("/nav", nav)
router.use("/manager", manager)
router.use("/focus", focus)
router.use("/articleCate", articleCate)
router.use("/article", article)
router.use("/setting", setting)

module.exports = router